package kr.co.icebird.spring3.config.filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import kr.co.icebird.spring3.app.model.Member;

/**
 * Servlet Filter implementation class ResponseFilter
 */
public class ResponseFilter implements Filter {

	private FilterConfig config = null;

	public ResponseFilter() {
		// TODO Auto-generated constructor stub
	}

	public void destroy() {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("REQ");
		Enumeration en01 = request.getAttributeNames();
		int i = 0;
		while (en01.hasMoreElements()) {
			System.out.println("REQ " + i + "== > " + en01.nextElement());
			i++;
		}

		chain.doFilter(request, response);
		System.out.println("RES" );
		Enumeration en02 = request.getAttributeNames();
		i = 0;
		while (en02.hasMoreElements()) {
			String attname = (String)en02.nextElement();
			System.out.println("RES " + i + "== > " + attname);
			System.out.println(request.getAttribute(attname).getClass());
			if(request.getAttribute(attname).getClass().equals((new ArrayList()).getClass())){
				
				for(int j=0; j < ((ArrayList)request.getAttribute(attname)).size(); j++ ){
					System.out.println("B  : "+((Member)((ArrayList)request.getAttribute(attname)).get(j)).getAddress());
					
					//((Member)((ArrayList)request.getAttribute(attname)).get(j)).setAddress(((Member)((ArrayList)request.getAttribute(attname)).get(j)).getAddress().replaceAll("\"", "&quto;"));
					
					System.out.println("A  : "+((Member)((ArrayList)request.getAttribute(attname)).get(j)).getAddress());
				}
				System.out.println("Array 입니다.");
			}
			i++;
		}
	}

	public void init(FilterConfig fConfig) throws ServletException {
		this.config = config;
	}

}
